package com.easy.web.service.impl;

import com.easy.web.mapper.SysConfigMapper;
import com.easy.web.model.SysConfig;
import com.easy.web.model.SysConfigExample;
import com.easy.web.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/12/26.
 */
@Service
@Transactional
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;
    /**
     * 获取系统配置列表
     *
     * @return
     */
    @Override
//    @Cacheable(value = "SysConfig",key="0")
    public List<SysConfig> getSysConfigList() {
        SysConfigExample sysConfigExample =new SysConfigExample();
        sysConfigExample.setOrderByClause("create_time");
        List<SysConfig> sysConfigList = sysConfigMapper.selectByExample(sysConfigExample);

        return sysConfigList;
    }

    /**
     * 更新基本配置
     */
    @Override
//    @CacheEvict(value = "SysConfig",allEntries = true)
    public List<SysConfig> updateBaseSysConfig(SysConfig sysConfig) {
        SysConfigExample sysConfigExample = new SysConfigExample();
        sysConfigExample.createCriteria().andConfigNameEqualTo(sysConfig.getConfigName());
        sysConfigMapper.updateByExampleSelective(sysConfig,sysConfigExample);
        return sysConfigMapper.selectByExample(new SysConfigExample());
    }

    /**
     * 查询某个配置
     *
     * @param configKey
     */
    @Override
//    @Cacheable(value="SysConfig",key = "#configKey")
    public SysConfig querySysConfigByConfigName(String configKey) {
        SysConfigExample sysConfigExample =new SysConfigExample();
        sysConfigExample.createCriteria().andConfigNameEqualTo(configKey);
        List<SysConfig> sysConfigList = sysConfigMapper.selectByExample(sysConfigExample);
        if(sysConfigList.size()>0){
            return sysConfigList.get(0);
        }
        return null;
    }
}
